<template>
	<view>
		<button type="primary" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">绑定手机号，修改资料</button>
	</view>
</template>

<script>
	import { mapActions } from 'vuex';
	export default {
		data() {
			return {
				code:''
			}
		},
		onLoad(){
			uni.login({
				success:(res)=>{
					console.log(res);
					if(res.errMsg == "login:ok"){
						this.code = res.code;
						}},
				fail:(err)=>{
					console.log('err',err);
					uni.showToast({
						title:'微信登录失败',
						icon:'none',
						duration:1500
					})
				}
			})
		},
		methods: {
			...mapActions(['updateMobile']),
			decryptPhoneNumber(e){
				if(!e.detail.encryptedData){
					uni.showToast({
						title:'授权失败',
						icon:'none'
					})
				}else{
					let payload = {
							code:this.code,
							encryptedData:e.detail.encryptedData,
							iv:e.detail.iv
						}
						this.updateMobile(payload).then(res=>{
							console.log('res',res)
							if(res.code == 0){
								uni.showToast({
									title:'绑定成功',
									icon:'success',
									duration:1500,
									success:()=>{
										setTimeout(function(){
											uni.switchTab({
												url:'/pages/user/user'
											})
										},1500)
									}
								})
							}
						})
					}
				}
			}
	}
</script>

<style lang="scss">

</style>
